﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WuDada.Core.Post;
using WuDada.Core.Post.Service;
using WuDada.Core.Post.Domain;

public partial class learning_test : System.Web.UI.Page
{
    private PostFactory m_PostFactory;
    private IPostService m_PostService;
    private IAnswerService m_AnswerService { get; set; }
    private SessionHelper m_SessionHelper;
    private int m_PostId = 0;

    private int m_Mode
    {
        get { if (ViewState["mode"] == null) { ViewState["mode"] = 0; } return int.Parse(ViewState["mode"].ToString()); }
        set { ViewState["mode"] = value; }
    }

    private int m_Page
    {
        get { if (ViewState["page"] == null) { ViewState["page"] = 0; } return int.Parse(ViewState["page"].ToString()); }
        set { ViewState["page"] = value; }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        m_PostFactory = new PostFactory();
        m_PostService = m_PostFactory.GetPostService();
        m_AnswerService = m_PostFactory.GetAnswerService();
        m_SessionHelper = new SessionHelper();
        m_PostId = QueryStringHelper.GetInteger("post", 0);

        if (m_SessionHelper.Member == null)
        {
            ScriptManager.RegisterClientScriptBlock(Page, Page.GetType(), "js", JavascriptUtil.AlertJSAndRedirect("請先登入會員。", "login.aspx"), false);
            return;
        }

        if (!IsPostBack)
        {            
            m_Mode = 0;
            m_Page = 0;
            hdnDetailId.Value = string.Empty;
            btnPre.Visible = false;
            btnOk.Visible = false;

            PostVO postVO = m_PostService.GetPostByIdNoLazy(m_PostId);
            if (postVO != null)
            {
                ltlPostTitle.Text = postVO.Title;
                AnswerVO answerVO = m_AnswerService.CreateEmptyAnswer(m_PostId, 20, m_SessionHelper.Member);

                if (answerVO == null)
                {
                    ScriptManager.RegisterClientScriptBlock(Page, Page.GetType(), "js", JavascriptUtil.AlertJSAndRedirect("尚未開放測驗。", "learning.aspx"), false);
                }
                else
                {
                    m_Mode = answerVO.AnswerId;
                    LoadAnswerDetail();
                }
            }
            else
            {
                Response.Redirect("learning.aspx");
            }
        }
    }

    private void LoadAnswerDetail()
    {
        if (m_Page <= 0)
        {
            btnPre.Visible = false;
        }
        else
        {
            btnPre.Visible = true;
        }

        if (m_Page >= 19)
        {
            btnOk.Visible = true;
            btnNext.Visible = false;
        }
        else
        {
            btnOk.Visible = false;
            btnNext.Visible = true;
        }
        rdoOptions.Items.Clear();
        ltlQuestion.Text = string.Empty;

        IDictionary<string, string> conditions = new Dictionary<string, string>();
        conditions.Add("AnswerId", m_Mode.ToString());
        conditions.Add("Flag", "1");
        conditions.Add("PageIndex", m_Page.ToString());
        conditions.Add("PageSize", "1");

        IList<AnswerDetailVO> list = m_AnswerService.GetAnswerDetailList(conditions);

        if (list != null && list.Count > 0)
        {
            AnswerDetailVO answerDetailVO = list[0];
            ltlQuestionNumber.Text = (m_Page + 1).ToString();
            ltlQuestion.Text = answerDetailVO.Question;
            hdnDetailId.Value = answerDetailVO.AnswerDetailId.ToString(); ;
            rdoOptions.Items.Add(new ListItem(answerDetailVO.Option1, answerDetailVO.Option1));
            rdoOptions.Items.Add(new ListItem(answerDetailVO.Option2, answerDetailVO.Option2));
            rdoOptions.Items.Add(new ListItem(answerDetailVO.Option3, answerDetailVO.Option3));
            rdoOptions.Items.Add(new ListItem(answerDetailVO.Option4, answerDetailVO.Option4));
            if (!string.IsNullOrEmpty(answerDetailVO.UserAnswer))
            {
                rdoOptions.SelectedValue = answerDetailVO.UserAnswer;
            }
        }
    }

    protected void btnPre_Click(object sender, EventArgs e)
    {
        if (m_Page > 0)
        {
            m_Page --;
        }
        rdoOptions.SelectedIndex = -1;

        LoadAnswerDetail();
    }

    private void UpdateAnswerDetail()
    {
        if (!string.IsNullOrEmpty(hdnDetailId.Value))
        {
            AnswerDetailVO answerDetailVO = m_AnswerService.GetAnswerDetailById(int.Parse(hdnDetailId.Value));
            if (answerDetailVO != null)
            {
                answerDetailVO.UserAnswer = rdoOptions.SelectedValue;
                answerDetailVO.UpdatedDate = DateTime.Now;
                m_AnswerService.UpdateAnswerDetail(answerDetailVO);
            }
        }
    }

    protected void btnNext_Click(object sender, EventArgs e)
    {
        UpdateAnswerDetail();
        if (m_Page < 19)
        {
            m_Page ++;
        }
        rdoOptions.SelectedIndex = -1;

        LoadAnswerDetail();
    }

    protected void btnOk_Click(object sender, EventArgs e)
    {
        UpdateAnswerDetail();
        m_AnswerService.UpdateFinishAnswer(m_AnswerService.GetAnswerById(m_Mode), 60);
        Response.Redirect(string.Format("learning_test_ok.aspx?answer={0}", m_Mode));
    }
}